package com.qfedu.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;


import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/jump");//登录页面
        shiroFilterFactoryBean.setSuccessUrl("/jumpadminindex"); //成功页面
        shiroFilterFactoryBean.setUnauthorizedUrl("jumpnotrolr");//没有权限页面
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterChainDefinitionMap.put("/assets/**", "anon");
        filterChainDefinitionMap.put("/jump", "anon");
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/jumpindex", "anon");
        filterChainDefinitionMap.put("/Login.html", "anon");

        filterChainDefinitionMap.put("/jumpregister", "anon");
        filterChainDefinitionMap.put("/register", "anon");
        filterChainDefinitionMap.put("/adminLogin", "anon");
          filterChainDefinitionMap.put("/mapper*","roles[superadmin]");
          filterChainDefinitionMap.put("/jumproleindex","roles[superadmin]");
          filterChainDefinitionMap.put("/jumpRoute","roles[admin]");
          filterChainDefinitionMap.put("/jumpgoodslist","roles[admin]");
          filterChainDefinitionMap.put("/jumpBoat","roles[admin]");
          filterChainDefinitionMap.put("/jumpbox","roles[admin]");
          filterChainDefinitionMap.put("/orderlist.to","roles[admin]");
        //主要这行代码必须放在所有权限设置的最后，不然会导致所有 url 都被拦截 剩余的都需要认证
        filterChainDefinitionMap.put("/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        defaultSecurityManager.setRealm(customRealm());
        return defaultSecurityManager;
    }


    @Bean
    public CustomerRealm customRealm() {
        CustomerRealm customRealm = new CustomerRealm();
        return customRealm;
    }

    /**
     * 用于thymeleaf模板使用shiro标签
     *
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }

}
